#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>

//体检学生信息结构体
struct Student{
	long long int number;		//学号
	char name[20];				//姓名
	char gender[20];			//性别
	char department[20];		//系别
	int age;					//年龄
	double height;				//身高
	double weight;				//体重
	double chest;				//胸围
	char date[20];				//日期
}pupil[1000];


//病例学生信息结构体
struct HealthRecord{
    long long int number;		//学号
    char name[50];				//姓名
    char gender[10];			//性别
    char department[50];		//系别
    char diagnosis[100];		//病例
    char date[20];				//日期
}illpupil[1000];

//添加健康学生体检信息
void addStudent()
{
	int i = 0;
	int num = 0;
	int add = 0;
	
	printf("请输入您想存入的人数:");
	scanf("%d", &add);
	
	FILE* fp = fopen("体检信息.txt","rb");
	
	while(!feof(fp))
	{	
		fread(&pupil[i] ,sizeof(struct Student) ,1 ,fp);
		i++;
		num++;
	}
	fclose(fp);
	
	fp = fopen("体检信息.txt","wb");
	
	for(i = 0 ; i<num+add ;i++)
	{
		if(i<num)
		{
			fwrite(&pupil[i] ,sizeof(struct Student) ,1 ,fp);
		}
		else
		{
			printf("\n请输入学号");
			scanf("%lld",&pupil[i].number);
			printf("\n请输入姓名");
			scanf("%s",&pupil[i].name);
			printf("\n请输入性别");
			scanf("%s",&pupil[i].gender);
			printf("\n请输入系别");
			scanf("%s",&pupil[i].department);
			printf("\n请输入年龄");
			scanf("%d",&pupil[i].age);
			printf("\n请输入身高");
			scanf("%lf",&pupil[i].height);
			printf("\n请输入体重");
			scanf("%lf",&pupil[i].weight);
			printf("\n请输入胸围");
			scanf("%lf",&pupil[i].chest);
			printf("\n请输入日期");
			scanf("%s",&pupil[i].date);
			fwrite(&pupil[i] ,sizeof(struct Student) ,1 ,fp);
		} 
	}
	fclose(fp);
}

//修改学生体检信息
void modifyStudent()
{
	int i = 0;
	int found;
	int num = 0;
	long long int studnum;
	int stat = 0;
	FILE* fp = fopen("体检信息.txt","rb");
	
	while(!feof(fp))
	{	
		fread(&pupil[i] ,sizeof(struct Student) ,1 ,fp);
		i++;
		num++;
	}
	fclose(fp);
	
	fp = fopen("体检信息.txt","wb");
	
	printf("请输入要修改的学生的学号：");
	
	scanf("%lld",&studnum);
	
	for(i = 0; i < num; i++)
	{
		if(pupil[i].number == studnum)
		{
			found = 1;
			printf("请输入新添加学生的信息：\n");
			printf("\n请输入学号");
			scanf("%lld",&pupil[i].number);
			printf("\n请输入姓名");
			scanf("%s",&pupil[i].name);
			printf("\n请输入性别");
			scanf("%s",&pupil[i].gender);
			printf("\n请输入系别");
			scanf("%s",&pupil[i].department);
			printf("\n请输入年龄");
			scanf("%d",&pupil[i].age);
			printf("\n请输入身高");
			scanf("%lf",&pupil[i].height);
			printf("\n请输入体重");
			scanf("%lf",&pupil[i].weight);
			printf("\n请输入胸围");
			scanf("%lf",&pupil[i].chest);
			printf("\n请输入日期");
			scanf("%s",&pupil[i].date);
			stat = 1;
		}
		fwrite(&pupil[i] ,sizeof(struct Student) ,1 ,fp);
	}
	fclose(fp);
	
	if(stat == 0)
	{
		printf("系统内无该学生信息,请重试");
		system("pause");
	}
}

//删除学生体检信息
void deleteStudent()
{
	int i = 0;
	int num = 0;
	long long int studnum = 0;
	int stat = 0;
	
	FILE* fp = fopen("体检信息.txt","rb");
	
	while(!feof(fp))
	{	
		fread(&pupil[i] ,sizeof(struct Student) ,1 ,fp);
		i++;
		num++;
	}
	fclose(fp);
	
	fp = fopen("体检信息.txt","wb");
	printf("\n\n\t\t请输入您想删除的学生学号:");
	scanf("%lld",&studnum);
	
	for(i = 0; i < num; i++)
	{
		if(pupil[i].number == studnum)
		{
			stat = 1;
		}
		else
		{
			fwrite(&pupil[i] ,sizeof(struct Student) ,1 ,fp);
		}	
	}
	fclose(fp); 

	
	if(stat == 1)
	{
		printf("该学生体检已删除");
		system("pause");
	}
	else
	{
		printf("系统内无该学生信息,请重试");
		system("pause");
	}
}

//查询学生体检信息
void searchStudentInfo()
{
	long long int studnum;
	int i; 
	int num = 0;
	int found = 0;
	
	FILE* fp = fopen("体检信息.txt","rb");
	
	while(!feof(fp))
	{	
		fread(&pupil[i] ,sizeof(struct Student) ,1 ,fp);
		i++;
		num++;
	}
	fclose(fp);

		printf("\n\n\t\t请输入您想查询的学生学号:");
		scanf("%lld",&studnum);
		
		for(i = 0; i < num; i++)
		{
			if (pupil[i].number == studnum)
			{
				found = 1;
				printf("\n\n\t\t该学生体检信息已被找到,信息如下\n");
		    	printf("\n\n\t\t该学生学号为： %lld\n", pupil[i].number);        
			    printf("\n\n\t\t该学生名字为： %s\n", pupil[i].name);
		        printf("\n\n\t\t该学生性别为： %s\n",pupil[i].gender);
		        printf("\n\n\t\t该学生系别为： %s\n", pupil[i].department);
                printf("\n\n\t\t该学生年龄为： %d\n", pupil[i].age);
	            printf("\n\n\t\t该学生身高为： %.2f\n", pupil[i].height);
		        printf("\n\n\t\t该学生体重为： %.2f\n", pupil[i].weight);
		        printf("\n\n\t\t该学生胸围为： %.2f\n", pupil[i].chest);
		        printf("\n\n\t\t日期： %s\n", pupil[i].date);
		       	system("pause");
		    }
		}	   
		if(!found)
		{
			printf("未查到该学生");	
			system("pause");
		}     
}
	
//统计学生体检信息
void calculateHealth()
{
	int num = 0;
    int i;
    int totalAge = 0;						//总成绩
    double totalHeight = 0;					//总身高
    double totalWeight = 0;					//总体重
    double totalChestCircumference = 0;		//总胸围

    int maleCount = 0;						//男生总人数
    int femaleCount = 0;					//女生总人数

	FILE* fp = fopen("体检信息.txt","rb");
	
	while(!feof(fp))
	{	
		fread(&pupil[i] ,sizeof(struct Student) ,1 ,fp);
		i++;
		num++;
	}
	fclose(fp);

    for (i = 0; i < num; i++)
	{
        totalAge += pupil[i].age;
        totalHeight += pupil[i].height;
        totalWeight += pupil[i].weight;
        totalChestCircumference += pupil[i].chest;

        if (strcmp(pupil[i].gender, "male") == 0)
		{
            maleCount++;
        } 
		else if (strcmp(pupil[i].gender, "female") == 0)
		{
            femaleCount++;
        }
    }

    double averageAge = (double)totalAge / num;
    double averageHeight = totalHeight / num;
    double averageWeight = totalWeight / num;
    double averageChestCircumference = totalChestCircumference / num;

    printf("\n\n\t\t学生的总体数目是： %d\n", num);
    printf("\n\n\t\t平均年龄: %.2lf\n", averageAge);
    printf("\n\n\t\t平均身高 :%.2lf\n", averageHeight);
    printf("\n\n\t\t平均体重: %.2f\n", averageWeight);
    printf("\n\n\t\t平均胸围: %.2f\n", averageChestCircumference);
    printf("\n\n\t\t男学生的总数: %d\n", maleCount);
    printf("\n\n\t\t女学生的总数: %d\n", femaleCount);
    system("pause");
}
    
     
    
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

//添加学生病例信息
void addillStudent()
{
	int i = 0;
	int num = 0;
	int add = 0;
	
	printf("请输入您想存入的人数:");
	scanf("%d", &add);
	
	FILE* fp = fopen("病例信息.txt","rb");
	
	while(!feof(fp))
	{	
		fread(&illpupil[i] ,sizeof(struct HealthRecord) ,1 ,fp);
		i++;
		num++;
	}
	fclose(fp);
	
	fp = fopen("病例信息.txt","wb");
	
	for(i = 0 ; i<num+add ;i++)
	{
		if(i<num)
		{
			fwrite(&illpupil[i] ,sizeof(struct HealthRecord) ,1 ,fp);
		}
		else
		{
			printf("\n请输入学号");
			scanf("%lld",&illpupil[i].number);
			printf("\n请输入姓名");
			scanf("%s",&illpupil[i].name);
			printf("\n请输入性别");
			scanf("%s",&illpupil[i].gender);
			printf("\n请输入系别");
			scanf("%s",&illpupil[i].department);
			printf("\n请输入病例");
			scanf("%s",&illpupil[i].diagnosis);
			printf("\n请输入日期");
			scanf("%s",&illpupil[i].date);
			fwrite(&illpupil[i] ,sizeof(struct HealthRecord) ,1 ,fp);
		} 
	}
	fclose(fp);
}

//修改学生病例信息
void modifyillStudent()
{
	int i = 0;
	int found;
	int num = 0;
	long long int studnum;
	int stat = 0;
	FILE* fp = fopen("病例信息.txt","rb");
	
	while(!feof(fp))
	{	
		fread(&illpupil[i] ,sizeof(struct HealthRecord) ,1 ,fp);
		i++;
		num++;
	}
	fclose(fp);
	
	fp = fopen("病例信息.txt","wb");
	
	printf("请输入要修改的学生的学号：");
	
	scanf("%lld",&studnum);
	
	for(i = 0; i < num; i++)
	{
		if(illpupil[i].number == studnum)
		{
			found = 1;
			printf("\n请输入学号");
			scanf("%lld",&illpupil[i].number);
			printf("\n请输入姓名");
			scanf("%s",&illpupil[i].name);
			printf("\n请输入性别");
			scanf("%s",&illpupil[i].gender);
			printf("\n请输入系别");
			scanf("%s",&illpupil[i].department);
			printf("\n请输入病例");
			scanf("%s",&illpupil[i].diagnosis);
			printf("\n请输入日期");
			scanf("%s",&illpupil[i].date);
			stat = 1;
		}
		fwrite(&illpupil[i] ,sizeof(struct HealthRecord) ,1 ,fp);
	}
	fclose(fp);
	if(stat == 0)
	{
		printf("系统内无该学生信息,请重试");
		system("pause");
	}
}

//删除学生病例信息
void deleteillStudent()
{
	int i = 0;
	int num = 0;
	long long int studnum = 0;
	int stat = 0;
	
	FILE* fp = fopen("病例信息.txt","rb");
	
	while(!feof(fp))
	{	
		fread(&illpupil[i] ,sizeof(struct HealthRecord) ,1 ,fp);
		i++;
		num++;
	}
	fclose(fp);
	
	fp = fopen("病例信息.txt","wb");
	printf("\n\n\t\t请输入您想删除的学生学号:");
	scanf("%lld",&studnum);
	for(i = 0; i < num; i++)
	{
		if(illpupil[i].number == studnum)
		{
			stat = 1;
		}
		else
		{
			fwrite(&illpupil[i] ,sizeof(struct HealthRecord) ,1 ,fp);
		}	
	}
	fclose(fp); 
	if(stat == 1)
	{
		printf("该学生病例信息已删除");
		system("pause");
	}
	else
	{
		printf("系统内无该学生病例信息,请重试");
		system("pause");
	}
}

//查询学生病例信息
void searchillStudentInfo()
{
	long long int studnum;
	int i; 
	int num = 0;
	int found = 0;
	
	FILE* fp = fopen("病例信息.txt","rb");
	
	while(!feof(fp))
	{	
		fread(&illpupil[i] ,sizeof(struct HealthRecord) ,1 ,fp);
		i++;
		num++;
	}
	fclose(fp);

		printf("\n\n\t\t请输入您想查询的学生学号:");
		scanf("%lld",&studnum);
		for(i = 0; i < num; i++)
		{
		
			 if (illpupil[i].number == studnum)
			{
				found = 1;
				printf("\n\n\t\t该学生信息已被找到,信息如下\n");
		    	printf("\n\n\t\t该学生学号为： %lld\n", illpupil[i].number);        
			    printf("\n\n\t\t该学生名字为： %s\n", illpupil[i].name);
		        printf("\n\n\t\t该学生性别为： %s\n",illpupil[i].gender);
		        printf("\n\n\t\t该学生系别为： %s\n", illpupil[i].department);
		        printf("\n\n\t\t该学生病例为： %s\n", illpupil[i].diagnosis);
		        printf("\n\n\t\t日期: %s\n", illpupil[i].date);
		    }
		}	        
		if(!found)
		{
			printf("未查到该学生");
		}
		system("pause");
}









 